// UVa1160 X-Plosives
// 陈锋
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 4;
int Pa[MAXN]; // 并查集
int findPa(int u) { return Pa[u] == u ? u : (Pa[u] = findPa(Pa[u])); }

int main() {
  while (true) {
    for(int i = 0; i <= MAXN; i++) Pa[i] = i;
    int ans = 0, u, v;
    while (true) {
      if (scanf("%d", &u) != 1) return 0;
      if (u == -1) break;
      scanf("%d", &v), u = findPa(u), v = findPa(v);
      if (u == v) ans++;
      else Pa[u] = v;
    }
    printf("%d\n", ans);
  }
  return 0;
}
/*
算法分析请参考: 《入门经典训练指南-升级版》3.1.3节 例题5
*/
// 25932346  1160  X-Plosives  Accepted  C++  0.000  2021-01-09 03:02:01